//
//  XJNetworkTools.m
//  weiboOC
//
//  Created by 徐绩 on 16/4/18.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "XJNetworkTools.h"

@implementation XJNetworkTools

+(instancetype)sharedTools{

    static XJNetworkTools *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc]init];
        
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

    });
    return instance;
}

- (void)request:(XJRequestMethod)method urlString:(NSString *)urlString parameters:(id)parameters finished:(void (^)(id, NSError *))finished{
    
    if (method == XJRequestMethodGet) {
        [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 成功的block
            finished(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            // 失败的话，回调错误信息
            finished(nil, error);
        }];
    }else{
        [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 成功的block
            finished(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            // 失败的话，回调错误信息
            finished(nil, error);
        }];
    }
}

@end
